package com.sunhao.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {

    private String mDatabaseName;
    private TextView tv_reslut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        findViewById(R.id.db_add).setOnClickListener(this);
        findViewById(R.id.db_delete).setOnClickListener(this);

        tv_reslut = findViewById(R.id.tv_reslut);

        //生成一个测试数据库完整路径
        mDatabaseName = getFilesDir()+"/test.db";
    }

    @Override
    public void onClick(View view) {
            switch (view.getId()){
                case R.id.db_add:
                    //新建数据库,如果数据库不存在就创建它
                    SQLiteDatabase db = openOrCreateDatabase(mDatabaseName, Context.MODE_PRIVATE, null);
                    String desc =String.format("数据库%s创建%s",db.getPath(),(db!=null?"成功":"失败"));
                    tv_reslut.setText(desc);
                    break;
                case  R.id.db_delete:
                    //删除当前数据库
                    Boolean result = deleteDatabase(mDatabaseName);
                     desc =String.format("数据库%s删除%s",mDatabaseName,(result?"成功":"失败"));
                    tv_reslut.setText(desc);
                    break;
            }
    }
}